//
//  XQDeviceViewController.m
//  XQBluetooth
//
//  Created by 包月兴 on 16/6/17.
//  Copyright © 2016年 包月兴. All rights reserved.
//

#import "XQDeviceViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import "NSDate+XQExpend.h"
#import "NSTimer+XQBlcokSupport.h"
#import "XQBluetoothSDK.h"
#import "AKTKit.h"
#import "XQNotificationView.h"
#import "XQAppViewController.h"
#import "MNPhotoGraphViewCtrl.h"
#import "XQStatisticsViewController.h"
#import "XQUpgradeViewController.h"
@interface  XQDeviceViewController()
@property (nonatomic,copy) UILabel *alertMessage;
@property (nonatomic,strong)  XQBleTotalSteps gainCurrentStepBlock;
@property (nonatomic,strong)  XQBleTotalDistance gainCurrentDistanceBlock;
@property (nonatomic,strong)  XQBleTotalCalorie gainCurrentCalorieBlock;
@property (nonatomic,strong)  XQBleTotalSleep gainCurrentSleepDurationBlock;
@property (nonatomic,strong)  XQBleElectric gainElectricPercentBlock;
@property (nonatomic,strong)  XQBleRSSI rssiBlock;
@property (nonatomic,strong) XQBleFindCellphone findPhoneBlock;
@property (nonatomic,strong) XQBleRunSteps runStepsBlock;
@property (nonatomic,strong) XQBleHeart  heartBlock;


@property (nonatomic,copy) UILabel *currentElectricLabel; //实时电量
@property (nonatomic,copy) UILabel *currentStepLabel; //实时步数
@property (nonatomic,copy) UILabel *currentSleepLabel; //睡眠时长
@property (nonatomic,copy) UILabel *currentDistanceLabel; //实时里程
@property (nonatomic,copy) UILabel *currentCalorieLabel; //实时卡路里
@property (nonatomic,copy) UILabel *currentRssiLabel; //实时蓝牙信号
@property (nonatomic,copy) UILabel *currentRunStepsLabel; //实时跑步步数
@property (nonatomic,copy) UILabel *currentHeartLabel; //实时心率

@property (nonatomic,copy) UIButton *syncMeasureHeartBtn; //心率按钮
@property (nonatomic,copy) UIButton *syncFirmwareMessageBtn;
@property (nonatomic,copy) UIButton *syncShakeTimeBtn;
@property (nonatomic,copy) UIButton *syncClockBtn;
@property (nonatomic,copy) UIButton *syncPersonMessageBtn;
@property (nonatomic,copy) UIButton *syncSedentarinessBtn;
@property (nonatomic,copy) UIButton *syncDisconnectBtn;
@property (nonatomic,copy) UIButton *syncAppBtn;
@property (nonatomic,copy) UIButton *syncPhotographBtn;
@property (nonatomic,copy) UIButton *syncStepAndSleepDetailsBtn;
@property (nonatomic,copy) UIButton *syncUnbindBtn;
@property (nonatomic,copy) UIButton *upgradeBtn;
@property (nonatomic,copy) UIButton *reconnectBtn;
@property (nonatomic,copy) UIButton *syncGoalStepBtn;
@property (nonatomic,copy) UIButton *syncBrightScreenBtn;
@property (nonatomic,copy) UIButton *syncRestBtn;
@end

@implementation XQDeviceViewController
-(void)viewDidLoad
{
    [super viewDidLoad];
    [self p_initUI];
    [self p_initFunction];
    [self p_initBlock];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reconnect) name:XQBLEDISCONNECTNOTI object:nil];

}


-(void)p_initFunction
{
    if (_peripheral) {
        //未绑定设备
        _alertMessage.text = @"正在绑定设备....";
        [XQBleSDKInstance connectPeripheralWithDuration:5 peripheral:_peripheral success:^{
            _alertMessage.text = @"绑定成功.......";
            [NSTimer scheduledTimerWithTimeInterval:10 block:^{
                _alertMessage.text = @"正在同步数据.......";
                //绑定设备
                [XQBleSDKInstance bindWithPeripheral:_peripheral syncComplete:nil syncFailure:nil];
                //开启睡眠时间段测量心率
                [XQBleSDKInstance syncTimingMeasurementHeartWithType:XQBluetoothFunSwitchOpen startHour:21 startMin:0 endHour:9 endMin:0 success:nil failure:nil];
            } repeats:NO];
        } failure:^(XQBluetoothError *error) {
            _alertMessage.text = [NSString stringWithFormat:@"绑定设备失败:%@",[error description]];
        }];
        
    }else
    {
        //已经绑定
        _alertMessage.text = @"正在连接已经绑定的设备....";
        [XQBleSDKInstance connectBindedPeripheralWithDuration:5 success:^{
            _alertMessage.text = @"连接成功";
        } failure:^(XQBluetoothError *error) {
             _alertMessage.text = [NSString stringWithFormat:@"绑定设备失败:%@",[error description]];
        } syncComplete:nil syncFailure:nil];
        
    }
}

-(void)p_initUI
{
    _alertMessage = self.alertMessage;
    _currentHeartLabel = self.currentHeartLabel;
    _currentElectricLabel = self.currentElectricLabel;
    _currentStepLabel = self.currentStepLabel;
    _currentSleepLabel = self.currentSleepLabel;
    _currentDistanceLabel = self.currentDistanceLabel;
    _currentCalorieLabel = self.currentCalorieLabel;
    _currentRssiLabel = self.currentRssiLabel;
    _currentRunStepsLabel = self.currentRunStepsLabel;
    
    _syncMeasureHeartBtn = self.syncMeasureHeartBtn;
    _syncFirmwareMessageBtn = self.syncFirmwareMessageBtn;
    _syncShakeTimeBtn = self.syncShakeTimeBtn;
    _syncClockBtn = self.syncClockBtn;
    _syncPersonMessageBtn = self.syncPersonMessageBtn;
    _syncSedentarinessBtn = self.syncSedentarinessBtn;
    _syncDisconnectBtn = self.syncDisconnectBtn;
    _syncAppBtn = self.syncAppBtn;
    _syncPhotographBtn = self.syncPhotographBtn;
    _syncStepAndSleepDetailsBtn = self.syncStepAndSleepDetailsBtn;
    _syncUnbindBtn = self.syncUnbindBtn;
    _syncGoalStepBtn = self.syncGoalStepBtn;
    _upgradeBtn = self.upgradeBtn;
    _syncBrightScreenBtn = self.syncBrightScreenBtn;
    _syncRestBtn = self.syncRestBtn;
    
}


-(void)p_initBlock
{
    XQBleSDKInstance.totalStepsBlock = self.gainCurrentStepBlock;
    XQBleSDKInstance.totalDistanceBlock = self.gainCurrentDistanceBlock;
    XQBleSDKInstance.totalCalorieBlock  = self.gainCurrentCalorieBlock;
    XQBleSDKInstance.totalSleepBlock = self.gainCurrentSleepDurationBlock;
    XQBleSDKInstance.electricBlock= self.gainElectricPercentBlock;
    XQBleSDKInstance.findCellphoneBlock = self.findPhoneBlock;
    XQBleSDKInstance.runStepsBlock = self.runStepsBlock;
    XQBleSDKInstance.rssiBlock = self.rssiBlock;
    XQBleSDKInstance.runStepsBlock = self.runStepsBlock;
}


#pragma mark --- setters and getters
#pragma mark --- 提示信息
-(UILabel *)alertMessage
{
    if (_alertMessage == nil) {
        CGFloat y = mAKT_SCREENHEIGHT -90;
        _alertMessage = [[UILabel alloc] initWithFrame:CGRectMake(0,y, mAKT_SCREENWITTH, 30)];
        _alertMessage.textAlignment = NSTextAlignmentCenter;
        _alertMessage.textColor = mAKT_Color_White;
        _alertMessage.backgroundColor = mAKT_Color_Red;
        _alertMessage.font = mAKT_Font_Bold_12;
        [self.view addSubview:_alertMessage];
        
    }
    return _alertMessage;
}
#pragma mark ##############################labels#########################

#pragma mark --- 实时心率label

-(UILabel *)currentHeartLabel
{
    if (_currentHeartLabel == nil) {
        _currentHeartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,30,self.view.frame.size.width/2,30)];
        _currentHeartLabel.textAlignment = NSTextAlignmentCenter;
        _currentHeartLabel.font = [UIFont systemFontOfSize:10];
        _currentHeartLabel.text = @"实时心率:";
        _currentHeartLabel.textColor = mAKT_Color_Red;
        [self.view addSubview:_currentHeartLabel];
    }
    return _currentHeartLabel;
}

#pragma mark --- 实时电量label

-(UILabel *)currentElectricLabel
{
    if (_currentElectricLabel == nil) {
        _currentElectricLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2,30,self.view.frame.size.width/2,30)];
        _currentElectricLabel.textAlignment = NSTextAlignmentCenter;
        _currentElectricLabel.font = [UIFont systemFontOfSize:10];
        _currentElectricLabel.text = @"实时电量:";
        [self.view addSubview:_currentElectricLabel];
    }
    return _currentElectricLabel;
}

#pragma mark --- 实时步数label
-(UILabel *)currentStepLabel
{
    if (_currentStepLabel == nil) {
        _currentStepLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,70,self.view.frame.size.width/2,30)];
        _currentStepLabel.textAlignment = NSTextAlignmentCenter;
        _currentStepLabel.font = [UIFont systemFontOfSize:10];
        _currentStepLabel.text = @"实时步数:";
        [self.view addSubview:_currentStepLabel];
    }
    return _currentStepLabel;
}

#pragma mark --- 睡眠时长label
-(UILabel *)currentSleepLabel
{
    if (_currentSleepLabel == nil) {
        _currentSleepLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2,70,self.view.frame.size.width/2,30)];
        _currentSleepLabel.textAlignment = NSTextAlignmentCenter;
        _currentSleepLabel.font = [UIFont systemFontOfSize:10];
        _currentSleepLabel.text = @"睡眠时长:";
        [self.view addSubview:_currentSleepLabel];
    }
    return _currentSleepLabel;
}

#pragma mark --- 实时里程label
-(UILabel *)currentDistanceLabel
{
    if (_currentDistanceLabel == nil) {
        _currentDistanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,110,self.view.frame.size.width/2,30)];
        _currentDistanceLabel.textAlignment = NSTextAlignmentCenter;
        _currentDistanceLabel.font = [UIFont systemFontOfSize:10];
        _currentDistanceLabel.text = @"实时里程:";
        [self.view addSubview:_currentDistanceLabel];
    }
    return _currentDistanceLabel;
}

#pragma mark --- 实时卡路里label
-(UILabel *)currentCalorieLabel
{
    if (_currentCalorieLabel == nil) {
        _currentCalorieLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2,110,self.view.frame.size.width/2,30)];
        _currentCalorieLabel.textAlignment = NSTextAlignmentCenter;
        _currentCalorieLabel.font = [UIFont systemFontOfSize:10];
        _currentCalorieLabel.text = @"实时卡路里:";
        [self.view addSubview:_currentCalorieLabel];
    }
    return _currentCalorieLabel;
}

#pragma mark ---实时蓝牙信号label
-(UILabel *)currentRssiLabel
{
    if (_currentRssiLabel == nil) {
        _currentRssiLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,150,self.view.frame.size.width/2,30)];
        _currentRssiLabel.textAlignment = NSTextAlignmentCenter;
        _currentRssiLabel.font = [UIFont systemFontOfSize:10];
        _currentRssiLabel.text = @"实时蓝牙信号:";
        [self.view addSubview:_currentRssiLabel];
    }
    return _currentRssiLabel;
}

#pragma mark --- 实时跑步步数label
-(UILabel *)currentRunStepsLabel
{
    if (_currentRunStepsLabel == nil) {
        _currentRunStepsLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2,150,self.view.frame.size.width/2,30)];
        _currentRunStepsLabel.textAlignment = NSTextAlignmentCenter;
        _currentRunStepsLabel.font = [UIFont systemFontOfSize:10];
        _currentRunStepsLabel.text = @"实时跑步:";
        [self.view addSubview:_currentRunStepsLabel];
    }
    return _currentRunStepsLabel;
}

#pragma mark ##############################Buttons########################
-(UIButton *)syncMeasureHeartBtn
{
    if (_syncMeasureHeartBtn == nil) {
        _syncMeasureHeartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x = (self.view.frame.size.width/2 - 150)/2;
        _syncMeasureHeartBtn.layer.cornerRadius = 10;
        _syncMeasureHeartBtn.frame  = CGRectMake(x,190,150,20);
        _syncMeasureHeartBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncMeasureHeartBtn setTitle:@"测量心率" forState:UIControlStateNormal];
        _syncMeasureHeartBtn.tag = 100;
        [_syncMeasureHeartBtn addTarget:self action:@selector(syncMeasureHeartEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncMeasureHeartBtn];
        
    }
    return _syncMeasureHeartBtn;
}

-(UIButton *)syncFirmwareMessageBtn
{
    if (_syncFirmwareMessageBtn == nil) {
        _syncFirmwareMessageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (self.view.frame.size.width/2 - 150)/2 + self.view.frame.size.width/2;
        _syncFirmwareMessageBtn.frame  = CGRectMake(x,190,150,20);
        _syncFirmwareMessageBtn.layer.cornerRadius = 10;
        _syncFirmwareMessageBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncFirmwareMessageBtn setTitle:@"同步固件信息" forState:UIControlStateNormal];
        [_syncFirmwareMessageBtn addTarget:self action:@selector(syncFirmwareMessageEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncFirmwareMessageBtn];
    }
    return _syncFirmwareMessageBtn;
}


-(UIButton *)syncShakeTimeBtn
{
    if (_syncShakeTimeBtn == nil) {
        _syncShakeTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x = (self.view.frame.size.width/2 - 150)/2;
        _syncShakeTimeBtn.layer.cornerRadius = 10;
        _syncShakeTimeBtn.frame  = CGRectMake(x,230,150,20);
        _syncShakeTimeBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncShakeTimeBtn setTitle:@"同步3次震动" forState:UIControlStateNormal];
        [_syncShakeTimeBtn addTarget:self action:@selector(syncShakeTimeEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncShakeTimeBtn];
    }
    return _syncShakeTimeBtn;
}

-(UIButton *)syncClockBtn
{
    if (_syncClockBtn == nil) {
        _syncClockBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (self.view.frame.size.width/2 - 150)/2 + self.view.frame.size.width/2;
        _syncClockBtn.frame  = CGRectMake(x,230,150,20);
         _syncClockBtn.layer.cornerRadius = 10;
        _syncClockBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncClockBtn setTitle:@"同步3分钟后闹钟" forState:UIControlStateNormal];
        [_syncClockBtn addTarget:self action:@selector(syncClockEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncClockBtn];
    }
    return _syncClockBtn;
}

-(UIButton *)syncPersonMessageBtn
{
    if (_syncPersonMessageBtn == nil) {
        _syncPersonMessageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x = (self.view.frame.size.width/2 - 150)/2;
        _syncPersonMessageBtn.layer.cornerRadius = 10;
        _syncPersonMessageBtn.frame  = CGRectMake(x,270,150,20);
        _syncPersonMessageBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncPersonMessageBtn setTitle:@"同步170~70信息" forState:UIControlStateNormal];
        [_syncPersonMessageBtn addTarget:self action:@selector(syncPersonMessageEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncPersonMessageBtn];
    }
    return _syncPersonMessageBtn;
}

-(UIButton *)syncSedentarinessBtn
{
    if (_syncSedentarinessBtn == nil) {
        _syncSedentarinessBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (self.view.frame.size.width/2 - 150)/2 + self.view.frame.size.width/2;
        _syncSedentarinessBtn.frame  = CGRectMake(x,270,150,20);
        _syncSedentarinessBtn.layer.cornerRadius = 10;
        _syncSedentarinessBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncSedentarinessBtn setTitle:@"同步30分钟久坐" forState:UIControlStateNormal];
        [_syncSedentarinessBtn addTarget:self action:@selector(syncSedentarinessEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncSedentarinessBtn];
    }
    return _syncSedentarinessBtn;
}

-(UIButton *)syncDisconnectBtn
{
    if (_syncDisconnectBtn == nil) {
        _syncDisconnectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (self.view.frame.size.width/2 - 150)/2;
        _syncDisconnectBtn.frame  = CGRectMake(x,310,150,20);
        _syncDisconnectBtn.layer.cornerRadius = 10;
        _syncDisconnectBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncDisconnectBtn setTitle:@"打开断链提醒" forState:UIControlStateNormal];
        _syncDisconnectBtn.tag = 200;
        [_syncDisconnectBtn addTarget:self action:@selector(syncDisconnectEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncDisconnectBtn];
    }
    return _syncDisconnectBtn;
}

-(UIButton *)syncAppBtn
{
    if (_syncAppBtn == nil) {
        _syncAppBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (self.view.frame.size.width/2 - 150)/2+ self.view.frame.size.width/2;
        _syncAppBtn.frame  = CGRectMake(x,310,150,20);
        _syncAppBtn.layer.cornerRadius = 10;
        _syncAppBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncAppBtn setTitle:@"App提醒" forState:UIControlStateNormal];
        [_syncAppBtn addTarget:self action:@selector(syncAppEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncAppBtn];
    }
    return _syncAppBtn;
}

-(UIButton *)syncPhotographBtn
{
    if (_syncPhotographBtn == nil) {
        _syncPhotographBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (self.view.frame.size.width/2 - 150)/2;
        _syncPhotographBtn.frame  = CGRectMake(x,350,150,20);
        _syncPhotographBtn.layer.cornerRadius = 10;
        _syncPhotographBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncPhotographBtn setTitle:@"拍照" forState:UIControlStateNormal];
        [_syncPhotographBtn addTarget:self action:@selector(syncPhotographEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncPhotographBtn];
    }
    return _syncPhotographBtn;
}


-(UIButton *)syncStepAndSleepDetailsBtn
{
    if (_syncStepAndSleepDetailsBtn == nil) {
        _syncStepAndSleepDetailsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (self.view.frame.size.width/2 - 150)/2+ self.view.frame.size.width/2;
        _syncStepAndSleepDetailsBtn.frame  = CGRectMake(x,350,150,20);
        _syncStepAndSleepDetailsBtn.layer.cornerRadius = 10;
        _syncStepAndSleepDetailsBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncStepAndSleepDetailsBtn setTitle:@"获取运动和睡眠详情" forState:UIControlStateNormal];
        [_syncStepAndSleepDetailsBtn addTarget:self action:@selector(syncStepAndSleepDetailsEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncStepAndSleepDetailsBtn];
    }
    return _syncStepAndSleepDetailsBtn;
}


-(UIButton *)syncUnbindBtn
{
    if (_syncUnbindBtn == nil) {
        _syncUnbindBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (self.view.frame.size.width/2 - 150)/2;
        _syncUnbindBtn.frame  = CGRectMake(x,390,150,20);
        _syncUnbindBtn.layer.cornerRadius = 10;
        _syncUnbindBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncUnbindBtn setTitle:@"解绑" forState:UIControlStateNormal];
        [_syncUnbindBtn addTarget:self action:@selector(syncUnbindEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncUnbindBtn];
    }
    return _syncUnbindBtn;
}

-(UIButton *)syncGoalStepBtn
{
    if (_syncGoalStepBtn == nil) {
        _syncGoalStepBtn = [UIButton buttonWithType:UIButtonTypeCustom];
       CGFloat x= (self.view.frame.size.width/2 - 150)/2+ self.view.frame.size.width/2;
        _syncGoalStepBtn.frame  = CGRectMake(x,390,150,20);
        _syncGoalStepBtn.layer.cornerRadius = 10;
        _syncGoalStepBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncGoalStepBtn setTitle:@"设置目标-7000" forState:UIControlStateNormal];
        [_syncGoalStepBtn addTarget:self action:@selector(syncGoalStepEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncGoalStepBtn];
    }
    return _syncGoalStepBtn;
}


-(UIButton *)upgradeBtn
{
    if (_upgradeBtn == nil) {
        _upgradeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (self.view.frame.size.width/2 - 150)/2;
        _upgradeBtn.frame  = CGRectMake(x,430,150,20);
        _upgradeBtn.layer.cornerRadius = 10;
        _upgradeBtn.backgroundColor = [UIColor lightGrayColor];
        [_upgradeBtn setTitle:@"升级" forState:UIControlStateNormal];
        [_upgradeBtn addTarget:self action:@selector(syncUpgradeEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_upgradeBtn];
    }
    return _upgradeBtn;
}

-(UIButton *)syncBrightScreenBtn
{
    if (_syncBrightScreenBtn == nil) {
        _syncBrightScreenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
       CGFloat x= (self.view.frame.size.width/2 - 150)/2+ self.view.frame.size.width/2;
        _syncBrightScreenBtn.frame  = CGRectMake(x,430,150,20);
        _syncBrightScreenBtn.layer.cornerRadius = 10;
        _syncBrightScreenBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncBrightScreenBtn setTitle:@"打开抬手亮屏" forState:UIControlStateNormal];
        [_syncBrightScreenBtn addTarget:self action:@selector(syncBrightScreenEvent:) forControlEvents:UIControlEventTouchUpInside];
        _syncBrightScreenBtn.tag = 300;
        [self.view addSubview:_syncBrightScreenBtn];
    }
    return _syncBrightScreenBtn;
}


-(UIButton *)syncRestBtn
{
    if (_syncRestBtn == nil) {
        _syncRestBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (self.view.frame.size.width/2 - 150)/2;
        _syncRestBtn.frame  = CGRectMake(x,460,150,20);
        _syncRestBtn.layer.cornerRadius = 10;
        _syncRestBtn.backgroundColor = [UIColor lightGrayColor];
        [_syncRestBtn setTitle:@"重置设备" forState:UIControlStateNormal];
        [_syncRestBtn addTarget:self action:@selector(syncRestEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_syncRestBtn];
    }
    return _syncRestBtn;
}

-(UIButton *)reconnectBtn
{
    if (_reconnectBtn == nil) {
        _reconnectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (self.view.frame.size.width/2 - 150)/2 + self.view.frame.size.width/2 ;
        _reconnectBtn.frame  = CGRectMake(x,460,150,20);
        _reconnectBtn.layer.cornerRadius = 10;
        _reconnectBtn.backgroundColor = [UIColor lightGrayColor];
        [_reconnectBtn setTitle:@"重连" forState:UIControlStateNormal];
        [_reconnectBtn addTarget:self action:@selector(reconnectEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_reconnectBtn];
    }
    return _reconnectBtn;
}

#pragma mark ##############################blocks#########################


#pragma mark ----找手机--------
-(XQBleFindCellphone)findPhoneBlock
{
    if (_findPhoneBlock == nil) {
        _findPhoneBlock = ^()
        {
            //一般设置铃声,这里用文字表示洛
            [XQNotificationView showWithActiveStyle:NO andMessage:@"找到手机落"];
        };
    }
    return _findPhoneBlock;
}

-(XQBleTotalSteps)gainCurrentStepBlock
{
    __weak __typeof(&*self)weakSelf = self;
    if (_gainCurrentStepBlock == nil) {
        _gainCurrentStepBlock = ^(NSInteger steps)
        {
           
            weakSelf.currentStepLabel.text = [NSString stringWithFormat:@"实时步数:%ld",(long)steps];
        };
    }
    return _gainCurrentStepBlock;
}

-(XQBleTotalCalorie)gainCurrentCalorieBlock
{
    __weak __typeof(&*self)weakSelf = self;
    if (_gainCurrentCalorieBlock == nil) {
        _gainCurrentCalorieBlock = ^(NSInteger calorie)
        {
            
            weakSelf.currentCalorieLabel.text = [NSString stringWithFormat:@"实时消耗热量:%ld大卡",(long)calorie];
        };
    }
    return _gainCurrentCalorieBlock;
}

-(XQBleTotalDistance)gainCurrentDistanceBlock
{
    __weak __typeof(&*self)weakSelf = self;
    if (_gainCurrentDistanceBlock == nil) {
        _gainCurrentDistanceBlock = ^(NSInteger distance)
        {
            
            weakSelf.currentDistanceLabel.text = [NSString stringWithFormat:@"实时里程:%ld米",(long)distance];
        };
    }
    return _gainCurrentDistanceBlock;
}

-(XQBleTotalSleep)gainCurrentSleepDurationBlock
{
    __weak __typeof(&*self)weakSelf = self;
    if (_gainCurrentSleepDurationBlock == nil) {
        _gainCurrentSleepDurationBlock = ^(NSInteger duration)
        {
            NSInteger hour = duration/3600;
            NSInteger min = (duration%3600)/60;
            NSInteger sec = (duration%3600)%60;
            weakSelf.currentSleepLabel.text = [NSString stringWithFormat:@"睡眠时长:%ld时%ld分%ld秒",(long)hour,(long)min,(long)sec];
        };
    }
    return _gainCurrentSleepDurationBlock;
}


-(XQBleElectric)gainElectricPercentBlock
{
    __weak __typeof(&*self)weakSelf = self;
    if (_gainElectricPercentBlock == nil) {
        _gainElectricPercentBlock = ^(NSInteger percent,BOOL isCharging)
        {
            
            NSString *text = @"未充电";
            if (isCharging) {
                text = @"充电中.......";
            }
            weakSelf.currentElectricLabel.text = [NSString stringWithFormat:@"电量:%ld%%--%@",(long)percent,text];
            
        };
    }
    return _gainElectricPercentBlock;
}

-(XQBleRSSI)rssiBlock
{
    __weak __typeof(&*self)weakSelf = self;
    if (_rssiBlock == nil) {
        _rssiBlock = ^(NSInteger rssi)
        {
            
            weakSelf.currentRssiLabel.text = [NSString stringWithFormat:@"蓝牙信号强度:%ld",(long)rssi];
        };
    }
    return _rssiBlock;
}

-(XQBleRunSteps)runStepsBlock
{
    __weak __typeof(&*self)weakSelf = self;
    if (_runStepsBlock == nil) {
        _runStepsBlock = ^(NSInteger steps)
        {
           
            weakSelf.currentRunStepsLabel.text = [NSString stringWithFormat:@"跑步步数:%ld步",(long)steps];

        };
    }
    return _runStepsBlock;
}

#pragma mark ##############################events#########################


-(void)syncMeasureHeartEvent:(UIButton *)btn
{
    if (btn.tag == 100) {
        btn.tag = 101;
        [_syncMeasureHeartBtn setTitle:@"关闭心率" forState:UIControlStateNormal];
        __weak __typeof(&*self)weakSelf = self;
        [XQBleSDKInstance syncOpenHeartWithDuration:600 constantlyHeart:^(NSInteger heart,XQHeartSection section) {
             weakSelf.currentHeartLabel.text = [NSString stringWithFormat:@"实时心率:%ld",(long)heart];
        } success:nil failure:nil];
    }else
    {
        btn.tag = 100;
        [_syncMeasureHeartBtn setTitle:@"测量心率" forState:UIControlStateNormal];
        [XQBleSDKInstance syncCloseHeartWithSuccess:nil failure:nil];
        [NSTimer scheduledTimerWithTimeInterval:5 block:^{
              self.currentHeartLabel.text = @"";
        } repeats:NO];
        
      
    }
}

-(void)syncFirmwareMessageEvent
{
    
    [XQBleSDKInstance syncFirmwareMessageWithSuccess:^(NSInteger firmwareVersion, BOOL isDress) {
        NSString *message = @"";
        if (!isDress) {
            message = [NSString stringWithFormat:@"固件版本:%ld--没穿戴",(long)firmwareVersion];
        }else
        {
            message = [NSString stringWithFormat:@"固件版本:%ld--穿戴",(long)firmwareVersion];
        }
        [XQNotificationView showWithActiveStyle:NO andMessage:message];
        
    } failure:^(XQBluetoothError *error) {
        NSString *message = [NSString stringWithFormat:@"同步固件信息失败:%@",[error description]];
        [XQNotificationView showWithActiveStyle:NO andMessage:message];
    }];
}

-(void)syncShakeTimeEvent
{
    [XQBleSDKInstance syncShakeTimeWithTime:3 success:^{
        [XQNotificationView showWithActiveStyle:NO andMessage:@"同步震动成功"];
    } failure:^(XQBluetoothError *error) {
        NSString *message = [NSString stringWithFormat:@"同步震动失败:%@",[error description]];
        [XQNotificationView showWithActiveStyle:NO andMessage:message];
    }];
}

-(void)syncClockEvent
{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval time = [zone secondsFromGMTForDate:date];
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    NSInteger now = [dateNow timeIntervalSince1970];
    NSInteger hour = [[NSDate ISO8601StringWithFormat:DateFormatTypeSix timeInterval:now] integerValue];
    NSUInteger min = [[NSDate ISO8601StringWithFormat:DateFormatTypeFree timeInterval:now] integerValue];
    [XQBleSDKInstance syncClockWithNumber:0 clockType:XQBluetoothFunSwitchOpen clockHour:hour clockMin:min+3 isSundayOpen:YES isMondayOpen:YES isTuesdayOpen:YES isWednesdayOpen:YES isThursdayOpen:YES isFridayOpen:YES isSaturdayOpen:YES isOnce:NO success:^{
        [XQNotificationView showWithActiveStyle:NO andMessage:@"设置闹钟成功"];
    } failure:^(XQBluetoothError *error) {
        [XQNotificationView showWithActiveStyle:NO andMessage:[NSString stringWithFormat:@"设置闹钟失败:%@",[error description]]];
    }];
}

-(void)syncPersonMessageEvent
{
    [XQBleSDKInstance syncPersonMessageWithGenderType:XQGenderTypeMan age:25 height:170 weight:70 unitType:XQUnitTypeMetric success:^{
        [XQNotificationView showWithActiveStyle:NO andMessage:@"同步个人信息成功"];
    }failure:^(XQBluetoothError * _Nonnull error) {
        [XQNotificationView showWithActiveStyle:NO andMessage:[NSString stringWithFormat:@"同步个人信息失败:%@",[error description]]];
    }];
}

-(void)syncSedentarinessEvent
{
    
    // 在上班时间(9:00~~18:00) 每隔30分钟
    
    [XQBleSDKInstance syncSedentarinessWithTime:30*60 startHour:9 startMin:0 endHour:18 endMin:0 success:^{
         [XQNotificationView showWithActiveStyle:NO andMessage:@"同步久坐成功"];
    } failure:^(XQBluetoothError *error) {
        [XQNotificationView showWithActiveStyle:NO andMessage:[NSString stringWithFormat:@"同步久坐失败:%@",[error description]]];
    }];
}

-(void)syncDisconnectEvent:(UIButton *)btn
{
    if (btn.tag == 200) {
        [XQBleSDKInstance syncDisconnectWarnWithType:XQBluetoothFunSwitchOpen success:^{
            [XQNotificationView showWithActiveStyle:NO andMessage:@"打开断链提醒成功"];
            btn.tag = 201;
            [_syncDisconnectBtn setTitle:@"关闭断链提醒" forState:UIControlStateNormal];
        } failure:^(XQBluetoothError *error) {
            [XQNotificationView showWithActiveStyle:NO andMessage:[NSString stringWithFormat:@"打开断链提醒失败:%@",[error description]]];
        }];
    }else
    {
        
        [XQBleSDKInstance syncDisconnectWarnWithType:XQBluetoothFunSwitchClose success:^{
            btn.tag = 200;
            [_syncDisconnectBtn setTitle:@"打开断链提醒" forState:UIControlStateNormal];
            [XQNotificationView showWithActiveStyle:NO andMessage:@"关闭断链提醒成功"];
        } failure:^(XQBluetoothError *error) {
            [XQNotificationView showWithActiveStyle:NO andMessage:[NSString stringWithFormat:@"关闭断链提醒失败:%@",[error description]]];
        }];
    }
}

-(void)syncAppEvent
{
    XQAppViewController *appViewController = [[XQAppViewController alloc] init];
    [self.navigationController pushViewController:appViewController animated:YES];
}


-(void)syncPhotographEvent
{
    MNPhotoGraphViewCtrl *vc = [[MNPhotoGraphViewCtrl alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

-(void)syncStepAndSleepDetailsEvent
{
    XQStatisticsViewController *vc =[[XQStatisticsViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)syncUnbindEvent
{
    [XQBleSDKInstance unbind];
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)syncGoalStepEvent
{
    [XQBleSDKInstance syncGoalStep:150 success:^{
         [XQNotificationView showWithActiveStyle:NO andMessage:@"设置运动目标成功"];
    } failure:^(XQBluetoothError *error) {
         [XQNotificationView showWithActiveStyle:NO andMessage:@"设置运动目标失败"];
    }];
}

-(void)syncBrightScreenEvent:(UIButton *)btn
{
    if (btn.tag == 300) {
        [XQBleSDKInstance syncBrightScreenWithSwitch:XQBluetoothFunSwitchOpen success:^{
            [XQNotificationView showWithActiveStyle:NO andMessage:@"打开抬手亮屏成功"];
            btn.tag = 301;
            [_syncBrightScreenBtn setTitle:@"关闭抬手亮屏" forState:UIControlStateNormal];
        } failure:^(XQBluetoothError *error) {
            [XQNotificationView showWithActiveStyle:NO andMessage:[NSString stringWithFormat:@"打开抬手亮屏失败:%@",[error description]]];
        }];
    }else
    {
        [XQBleSDKInstance syncBrightScreenWithSwitch:XQBluetoothFunSwitchClose success:^{
            [XQNotificationView showWithActiveStyle:NO andMessage:@"关闭抬手亮屏成功"];
            btn.tag = 300;
            [_syncBrightScreenBtn setTitle:@"打开抬手亮屏" forState:UIControlStateNormal];
        } failure:^(XQBluetoothError *error) {
            [XQNotificationView showWithActiveStyle:NO andMessage:[NSString stringWithFormat:@"关闭抬手亮屏失败:%@",[error description]]];
        }];
    }
}

-(void)syncRestEvent
{
    [XQBleSDKInstance syncReset];
}


-(void)syncUpgradeEvent
{
    XQUpgradeViewController *vc = [[XQUpgradeViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)reconnect
{
    _reconnectBtn = self.reconnectBtn;
    _alertMessage.text = @"蓝牙已断开,点击重连";
}

-(void)reconnectEvent
{
    [_reconnectBtn setEnabled:NO];
    _alertMessage.text = @"正在重连中";
    [XQBleSDKInstance connectBindedPeripheralWithDuration:5 success:^{
        _alertMessage.text = @"重连成功";
        [_reconnectBtn removeFromSuperview];
        _reconnectBtn = nil;
    } failure:^(XQBluetoothError *error) {
        _alertMessage.text = [NSString stringWithFormat:@"重连失败:%@",[error description]];
        [_reconnectBtn setEnabled:YES];
    } syncComplete:nil syncFailure:nil];
    
}




@end
